﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class TenantControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }
        public bool LoadPropertyTenants(int companyid, int propertyid, int leaseid)
        {
            hfPropertyID.Value = propertyid.ToString();
            hfLeaseID.Value = leaseid.ToString();

            bool bHasValue = true;
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            String sqlSELECT = "SELECT * FROM rs_tblTenant WHERE PropertyID=@PropertyID AND LeaseID=@LeaseID AND CompanyID=@CompanyID";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@propertyid";
            param.Value = propertyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@leaseid";
            param.Value = leaseid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            gvTenants.DataSource = ds;
            gvTenants.DataBind();
            myConnection.Close();

            if (gvTenants.Rows.Count == 0)
            {
                bHasValue = false;
                return bHasValue;
            }
            return bHasValue;
        }

        protected void gvTenants_Remove(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int tenantid = Convert.ToInt32(e.CommandArgument.ToString());
                int propertyid = int.Parse(hfPropertyID.Value);
                int leaseid = int.Parse(hfLeaseID.Value);

                if (companyid > 0 && tenantid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@TenantID", SqlDbType.Int, 0, ParameterDirection.Input, tenantid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, propertyid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblPropertyTenant WHERE PropertyID=@PropertyID AND TenantID=@TenantID AND CompanyId=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);

                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    string activetab = "4";
                    ucModalActionMessageControl.ShowModal("Tenant Deleted Successfully!", RedirectToCurrentURL(activetab), true);
                    LoadPropertyTenants(companyid, propertyid, leaseid);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        public void ShowModal(int companyid, int tenantid, int propertyid, int leaseid)
        {
            hfIsDirty.Value = "";
            hfLeaseID.Value = leaseid.ToString();
            hfPropertyID.Value = leaseid.ToString();

            if (companyid > 0 && tenantid > 0)
                GetTanantData(companyid, tenantid);
            mpeModalTenant.Show();
        }

        private void GetTanantData(int companyid, int tenantid)
        {

        }
        protected void btnModalOK_Click(Object sender, EventArgs e)
        {
            mpeModalTenant.Hide();
            if (hfIsDirty.Value == "1")
            {
                string activetab = "4";
                Response.Redirect(RedirectToCurrentURL(activetab));
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int propertyid = int.Parse(hfPropertyID.Value);
                string relationship = txtRelationship.Text;
                string firstname = txtFirstName.Text;
                string lastname = txtLastName.Text;
                string middlename = txtMiddleName.Text;
                string homephone = txtHomePhone.Text;
                string mobile = txtMobile.Text;
                string fax = txtFax.Text;
                string email = txtEmail.Text;

                int leaseid = 0; //88888888888888888888888888888888888888888888888

                string SQL = "";
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Relationship", SqlDbType.NVarChar, 15, ParameterDirection.Input, relationship);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@FirstName", SqlDbType.NVarChar, 50, ParameterDirection.Input, firstname);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@LastName", SqlDbType.NVarChar, 50, ParameterDirection.Input, lastname);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@MiddleName", SqlDbType.NVarChar, 50, ParameterDirection.Input, middlename);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@HomePhone", SqlDbType.NVarChar, 15, ParameterDirection.Input, homephone);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Mobile", SqlDbType.NVarChar, 15, ParameterDirection.Input, mobile);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Fax", SqlDbType.NVarChar, 15, ParameterDirection.Input, fax);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Email", SqlDbType.NVarChar, 50, ParameterDirection.Input, email);

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, propertyid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@LeaseID", SqlDbType.Int, 0, ParameterDirection.Input, leaseid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 30, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
                SQLHelper.ExecuteScalarCmd(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                //ucModalActionMessageControl.ShowModal("Tenant Added Successfully!", RedirectToCurrentURL("2"), true);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        protected void btnAddTenant_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int propertyid = hfPropertyID.Value == "" ? 0 : int.Parse(hfPropertyID.Value);
            int leaseid = hfLeaseID.Value == "" ? 0 : int.Parse(hfLeaseID.Value);

            ShowModal(userIdentity.RSCompanyId, 0, propertyid, leaseid);
        }
        private string RedirectToCurrentURL(string activetab)
        {
            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", activetab);
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            return url;
        }
    }
}